﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using OpenESSOConfigurator.PresentationLayer.EventBroker;

namespace OpenESSOConfigurator.PresentationLayer
{
    public abstract class PresentationModel<T> where T : class
    {
        protected T selectedItem;
        protected List<T> items;
        protected OpenESSOConfigurator.PresentationLayer.EventBroker.EventBroker eventBroker;

        abstract protected string GetPresentationModelName();
        abstract protected List<T> GetItems();
        public virtual List<T> Items
        {
            get { return GetItems(); }
            set
            {
                items = value;
                FireEvent("topic://" + GetPresentationModelName() + "/Item", items, null);
            }
        }

        public virtual T SelectedItem
        {
            get { return selectedItem; }
            set
            {
                selectedItem = value;
                FireEvent("topic://" + GetPresentationModelName() + "/SelectedItem", selectedItem, null);
            }
        }
        public void Reset()
        {
            Items = null;
            SelectedItem = default(T);
        }
        public void RegisterView(IView view)
        {
            eventBroker.Register(view);
        }
        public void UnregisterView(IView view)
        {
            eventBroker.Unregister(view);
        }
        public void FireEvent(string topic, object sender, EventArgs e)
        {
            eventBroker.Fire(topic, sender, e);
        }
        public void DisposeAll()
        {
            eventBroker.Dispose();
        }

    }
    public class PresentationModel
    {
        protected OpenESSOConfigurator.PresentationLayer.EventBroker.EventBroker eventBroker;
        protected List<IView> views;
        public void RegisterView(IView view)
        {
            eventBroker.Register(view);
            views.Add(view);
        }
        public void UnregisterView(IView view)
        {
            eventBroker.Unregister(view);
            views.Remove(view);
        }
        public void FireEvent(string topic, object sender, EventArgs e)
        {
            eventBroker.Fire(topic, sender, e);
        }
        public void DisposeAll()
        {
            eventBroker.Dispose();
        }

        public void UpdateViews()
        {
            foreach (IView v in views)
                v.Update(); 
        }

    }

}
